package com.hubu.parking.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hubu.parking.common.BaseResponse;
import com.hubu.parking.common.DeleteRequest;
import com.hubu.parking.common.ResultUtils;
import com.hubu.parking.entity.park.ParkingSpot;
import com.hubu.parking.model.dto.park.OrderSpotDto;
import com.hubu.parking.model.dto.park.PageListSpotRequest;
import com.hubu.parking.model.vo.KeyValue;
import com.hubu.parking.service.ParkingSpotService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 车位接口
 */
@RestController
@RequestMapping("/spot")
@Slf4j
public class SpotController {

    @Resource
    private ParkingSpotService spotService;

    /**
     * 分页查询
     * @param pageRequest 筛选参数
     * @return
     */
    @PostMapping("/list")
    public BaseResponse<Page<ParkingSpot>> listSpot(@RequestBody PageListSpotRequest pageRequest) {
        return ResultUtils.success(spotService.pageListParkingSpot(pageRequest));
    }

    /**
     * 查询
     * @param pageRequest 筛选参数
     * @return
     */
    @PostMapping("/listNoPage")
    public BaseResponse<List<ParkingSpot>> listSpotNoPage(@RequestBody PageListSpotRequest pageRequest) {
        return ResultUtils.success(spotService.listParkingSpot(pageRequest));
    }

    /**
     * 查询出第一个
     * @return
     */
    @PostMapping("/listPartition")
    public BaseResponse<List<String>> listPartition() {
        return ResultUtils.success(spotService.listPartition());
    }

    /**
     * 新增
     *
     * @param parkingSpot
     * @return
     */
    @PostMapping("add")
    public BaseResponse<Boolean> add(@RequestBody ParkingSpot parkingSpot) {
        boolean save = spotService.save(parkingSpot);
        return ResultUtils.success(save);
    }

    /**
     * 修改
     *
     * @param parkingSpot
     * @return
     */
    @PostMapping("update")
    public BaseResponse<Boolean> update(@RequestBody ParkingSpot parkingSpot) {
        boolean save = spotService.saveOrUpdate(parkingSpot);
        return ResultUtils.success(save);
    }

    /**
     * 删除
     *
     * @param deleteRequest
     * @return
     */
    @PostMapping("delete")
    public BaseResponse<Boolean> delete(@RequestBody DeleteRequest deleteRequest) {
        Integer deleteId = deleteRequest.getId();
        boolean b = spotService.removeById(deleteId);
        return ResultUtils.success(b);
    }

    /**
     * 列出车位状态列表
     * @return 状态列表
     */
    @PostMapping("/list/status")
    public BaseResponse<List<KeyValue<Integer>>> listSpotStatus() {
        return ResultUtils.success(spotService.listStatus());
    }


    /**
     * 预定车位
     *
     * @param orderSpotDto
     * @return
     */
    @PostMapping("order")
    public BaseResponse<Boolean> orderSpot(@RequestBody OrderSpotDto orderSpotDto) {
        boolean save = spotService.orderSpot(orderSpotDto);
        return ResultUtils.success(save);
    }

    /**
     * 快速预定车位-最近车位
     *
     * @param orderSpotDto 用户id
     * @return
     */
    @PostMapping("quickOrder")
    public BaseResponse<Boolean> quickOrderSpot(@RequestBody OrderSpotDto orderSpotDto) {
        boolean save = spotService.quickOrderSpot(orderSpotDto);
        return ResultUtils.success(save);
    }

    /**
     * 查询当前距离车位最近的是
     */
    @PostMapping("nearestSpot")
    public BaseResponse<ParkingSpot> queryNearestSpot() {
        ParkingSpot nearSpot = spotService.getNearSpot();
        return ResultUtils.success(nearSpot);
    }
}
